home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spambayes / compatsets.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  14.7 KB  |  532 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. """Classes to represent arbitrary sets (including sets of sets).
  5.  
  6. This module implements sets using dictionaries whose values are
  7. ignored.  The usual operations (union, intersection, deletion, etc.)
  8. are provided as both methods and operators.
  9.  
  10. Important: sets are not sequences!  While they support 'x in s',
  11. 'len(s)', and 'for x in s', none of those operations are unique for
  12. sequences; for example, mappings support all three as well.  The
  13. characteristic operation for sequences is subscripting with small
  14. integers: s[i], for i in range(len(s)).  Sets don't support
  15. subscripting at all.  Also, sequences allow multiple occurrences and
  16. their elements have a definite order; sets on the other hand don't
  17. record multiple occurrences and don't remember the order of element
  18. insertion (which is why they don't support s[i]).
  19.  
  20. The following classes are provided:
  21.  
  22. BaseSet -- All the operations common to both mutable and immutable
  23.     sets. This is an abstract class, not meant to be directly
  24.     instantiated.
  25.  
  26. Set -- Mutable sets, subclass of BaseSet; not hashable.
  27.  
  28. ImmutableSet -- Immutable sets, subclass of BaseSet; hashable.
  29.     An iterable argument is mandatory to create an ImmutableSet.
  30.  
  31. _TemporarilyImmutableSet -- Not a subclass of BaseSet: just a wrapper
  32.     around a Set, hashable, giving the same hash value as the
  33.     immutable set equivalent would have.  Do not use this class
  34.     directly.
  35.  
  36. Only hashable objects can be added to a Set. In particular, you cannot
  37. really add a Set as an element to another Set; if you try, what is
  38. actually added is an ImmutableSet built from it (it compares equal to
  39. the one you tried adding).
  40.  
  41. When you ask if `x in y' where x is a Set and y is a Set or
  42. ImmutableSet, x is wrapped into a _TemporarilyImmutableSet z, and
  43. what's tested is actually `z in y'.
  44.  
  45. """
  46. __all__ = [
  47.     'BaseSet',
  48.     'Set',
  49.     'ImmutableSet']
  50.  
  51. try:
  52.     (True, False)
  53. except NameError:
  54.     (True, False) = (1, 0)
  55.  
  56.  
  57. class BaseSet(object):
  58.     '''Common base class for mutable and immutable sets.'''
  59.     __slots__ = [
  60.         '_data']
  61.     
  62.     def __init__(self):
  63.         '''This is an abstract class.'''
  64.         if self.__class__ is BaseSet:
  65.             raise TypeError, 'BaseSet is an abstract class.  Use Set or ImmutableSet.'
  66.         
  67.  
  68.     
  69.     def __len__(self):
  70.         '''Return the number of elements of a set.'''
  71.         return len(self._data)
  72.  
  73.     
  74.     def __repr__(self):
  75.         """Return string representation of a set.
  76.  
  77.         This looks like 'Set([<list of elements>])'.
  78.         """
  79.         return self._repr()
  80.  
  81.     __str__ = __repr__
  82.     
  83.     def _repr(self, sorted = False):
  84.         elements = self._data.keys()
  85.         if sorted:
  86.             elements.sort()
  87.         
  88.         return '%s(%r)' % (self.__class__.__name__, elements)
  89.  
  90.     
  91.     def __iter__(self):
  92.         '''Return an iterator over the elements or a set.
  93.  
  94.         This is the keys iterator for the underlying dict.
  95.         '''
  96.         return self._data.iterkeys()
  97.  
  98.     
  99.     def __eq__(self, other):
  100.         self._binary_sanity_check(other)
  101.         return self._data == other._data
  102.  
  103.     
  104.     def __ne__(self, other):
  105.         self._binary_sanity_check(other)
  106.         return self._data != other._data
  107.  
  108.     
  109.     def copy(self):
  110.         '''Return a shallow copy of a set.'''
  111.         result = self.__class__()
  112.         result._data.update(self._data)
  113.         return result
  114.  
  115.     __copy__ = copy
  116.     
  117.     def __deepcopy__(self, memo):
  118.         '''Return a deep copy of a set; used by copy module.'''
  119.         deepcopy = deepcopy
  120.         import copy
  121.         result = self.__class__()
  122.         memo[id(self)] = result
  123.         data = result._data
  124.         value = True
  125.         for elt in self:
  126.             data[deepcopy(elt, memo)] = value
  127.         
  128.         return result
  129.  
  130.     
  131.     def __or__(self, other):
  132.         '''Return the union of two sets as a new set.
  133.  
  134.         (I.e. all elements that are in either set.)
  135.         '''
  136.         if not isinstance(other, BaseSet):
  137.             return NotImplemented
  138.         
  139.         result = self.__class__()
  140.         result._data = self._data.copy()
  141.         result._data.update(other._data)
  142.         return result
  143.  
  144.     
  145.     def union(self, other):
  146.         '''Return the union of two sets as a new set.
  147.  
  148.         (I.e. all elements that are in either set.)
  149.         '''
  150.         return self | other
  151.  
  152.     
  153.     def __and__(self, other):
  154.         '''Return the intersection of two sets as a new set.
  155.  
  156.         (I.e. all elements that are in both sets.)
  157.         '''
  158.         if not isinstance(other, BaseSet):
  159.             return NotImplemented
  160.         
  161.         if len(self) <= len(other):
  162.             little = self
  163.             big = other
  164.         else:
  165.             little = other
  166.             big = self
  167.         common = filter(big._data.has_key, little._data.iterkeys())
  168.         return self.__class__(common)
  169.  
  170.     
  171.     def intersection(self, other):
  172.         '''Return the intersection of two sets as a new set.
  173.  
  174.         (I.e. all elements that are in both sets.)
  175.         '''
  176.         return self & other
  177.  
  178.     
  179.     def __xor__(self, other):
  180.         '''Return the symmetric difference of two sets as a new set.
  181.  
  182.         (I.e. all elements that are in exactly one of the sets.)
  183.         '''
  184.         if not isinstance(other, BaseSet):
  185.             return NotImplemented
  186.         
  187.         result = self.__class__()
  188.         data = result._data
  189.         value = True
  190.         selfdata = self._data
  191.         otherdata = other._data
  192.         for elt in selfdata:
  193.             if elt not in otherdata:
  194.                 data[elt] = value
  195.                 continue
  196.         
  197.         for elt in otherdata:
  198.             if elt not in selfdata:
  199.                 data[elt] = value
  200.                 continue
  201.         
  202.         return result
  203.  
  204.     
  205.     def symmetric_difference(self, other):
  206.         '''Return the symmetric difference of two sets as a new set.
  207.  
  208.         (I.e. all elements that are in exactly one of the sets.)
  209.         '''
  210.         return self ^ other
  211.  
  212.     
  213.     def __sub__(self, other):
  214.         '''Return the difference of two sets as a new Set.
  215.  
  216.         (I.e. all elements that are in this set and not in the other.)
  217.         '''
  218.         if not isinstance(other, BaseSet):
  219.             return NotImplemented
  220.         
  221.         result = self.__class__()
  222.         data = result._data
  223.         otherdata = other._data
  224.         value = True
  225.         for elt in self:
  226.             if elt not in otherdata:
  227.                 data[elt] = value
  228.                 continue
  229.         
  230.         return result
  231.  
  232.     
  233.     def difference(self, other):
  234.         '''Return the difference of two sets as a new Set.
  235.  
  236.         (I.e. all elements that are in this set and not in the other.)
  237.         '''
  238.         return self - other
  239.  
  240.     
  241.     def __contains__(self, element):
  242.         """Report whether an element is a member of a set.
  243.  
  244.         (Called in response to the expression `element in self'.)
  245.         """
  246.         
  247.         try:
  248.             return element in self._data
  249.         except TypeError:
  250.             transform = getattr(element, '_as_temporarily_immutable', None)
  251.             if transform is None:
  252.                 raise 
  253.             
  254.             return transform() in self._data
  255.  
  256.  
  257.     
  258.     def issubset(self, other):
  259.         '''Report whether another set contains this set.'''
  260.         self._binary_sanity_check(other)
  261.         if len(self) > len(other):
  262.             return False
  263.         
  264.         otherdata = other._data
  265.         for elt in self:
  266.             if elt not in otherdata:
  267.                 return False
  268.                 continue
  269.         
  270.         return True
  271.  
  272.     
  273.     def issuperset(self, other):
  274.         '''Report whether this set contains another set.'''
  275.         self._binary_sanity_check(other)
  276.         if len(self) < len(other):
  277.             return False
  278.         
  279.         selfdata = self._data
  280.         for elt in other:
  281.             if elt not in selfdata:
  282.                 return False
  283.                 continue
  284.         
  285.         return True
  286.  
  287.     __le__ = issubset
  288.     __ge__ = issuperset
  289.     
  290.     def __lt__(self, other):
  291.         self._binary_sanity_check(other)
  292.         if len(self) < len(other):
  293.             pass
  294.         return self.issubset(other)
  295.  
  296.     
  297.     def __gt__(self, other):
  298.         self._binary_sanity_check(other)
  299.         if len(self) > len(other):
  300.             pass
  301.         return self.issuperset(other)
  302.  
  303.     
  304.     def _binary_sanity_check(self, other):
  305.         if not isinstance(other, BaseSet):
  306.             raise TypeError, 'Binary operation only permitted between sets'
  307.         
  308.  
  309.     
  310.     def _compute_hash(self):
  311.         result = 0
  312.         for elt in self:
  313.             result ^= hash(elt)
  314.         
  315.         return result
  316.  
  317.     
  318.     def _update(self, iterable):
  319.         data = self._data
  320.         if isinstance(iterable, BaseSet):
  321.             data.update(iterable._data)
  322.             return None
  323.         
  324.         if isinstance(iterable, dict):
  325.             data.update(iterable)
  326.             return None
  327.         
  328.         value = True
  329.         it = iter(iterable)
  330.         while True:
  331.             
  332.             try:
  333.                 for element in it:
  334.                     data[element] = value
  335.                 
  336.                 return None
  337.             continue
  338.             except TypeError:
  339.                 transform = getattr(element, '_as_immutable', None)
  340.                 if transform is None:
  341.                     raise 
  342.                 
  343.                 data[transform()] = value
  344.                 continue
  345.             
  346.  
  347.             None<EXCEPTION MATCH>TypeError
  348.  
  349.  
  350.  
  351. class ImmutableSet(BaseSet):
  352.     '''Immutable set class.'''
  353.     __slots__ = [
  354.         '_hashcode']
  355.     
  356.     def __init__(self, iterable = None):
  357.         '''Construct an immutable set from an optional iterable.'''
  358.         self._hashcode = None
  359.         self._data = { }
  360.         if iterable is not None:
  361.             self._update(iterable)
  362.         
  363.  
  364.     
  365.     def __hash__(self):
  366.         if self._hashcode is None:
  367.             self._hashcode = self._compute_hash()
  368.         
  369.         return self._hashcode
  370.  
  371.  
  372.  
  373. class Set(BaseSet):
  374.     ''' Mutable set class.'''
  375.     __slots__ = []
  376.     
  377.     def __init__(self, iterable = None):
  378.         '''Construct a set from an optional iterable.'''
  379.         self._data = { }
  380.         if iterable is not None:
  381.             self._update(iterable)
  382.         
  383.  
  384.     
  385.     def __hash__(self):
  386.         '''A Set cannot be hashed.'''
  387.         raise TypeError, "Can't hash a Set, only an ImmutableSet."
  388.  
  389.     
  390.     def __ior__(self, other):
  391.         '''Update a set with the union of itself and another.'''
  392.         self._binary_sanity_check(other)
  393.         self._data.update(other._data)
  394.         return self
  395.  
  396.     
  397.     def union_update(self, other):
  398.         '''Update a set with the union of itself and another.'''
  399.         self |= other
  400.  
  401.     
  402.     def __iand__(self, other):
  403.         '''Update a set with the intersection of itself and another.'''
  404.         self._binary_sanity_check(other)
  405.         self._data = (self & other)._data
  406.         return self
  407.  
  408.     
  409.     def intersection_update(self, other):
  410.         '''Update a set with the intersection of itself and another.'''
  411.         self &= other
  412.  
  413.     
  414.     def __ixor__(self, other):
  415.         '''Update a set with the symmetric difference of itself and another.'''
  416.         self._binary_sanity_check(other)
  417.         data = self._data
  418.         value = True
  419.         for elt in other:
  420.             if elt in data:
  421.                 del data[elt]
  422.                 continue
  423.             data[elt] = value
  424.         
  425.         return self
  426.  
  427.     
  428.     def symmetric_difference_update(self, other):
  429.         '''Update a set with the symmetric difference of itself and another.'''
  430.         self ^= other
  431.  
  432.     
  433.     def __isub__(self, other):
  434.         '''Remove all elements of another set from this set.'''
  435.         self._binary_sanity_check(other)
  436.         data = self._data
  437.         for elt in other:
  438.             if elt in data:
  439.                 del data[elt]
  440.                 continue
  441.         
  442.         return self
  443.  
  444.     
  445.     def difference_update(self, other):
  446.         '''Remove all elements of another set from this set.'''
  447.         self -= other
  448.  
  449.     
  450.     def update(self, iterable):
  451.         '''Add all values from an iterable (such as a list or file).'''
  452.         self._update(iterable)
  453.  
  454.     
  455.     def clear(self):
  456.         '''Remove all elements from this set.'''
  457.         self._data.clear()
  458.  
  459.     
  460.     def add(self, element):
  461.         '''Add an element to a set.
  462.  
  463.         This has no effect if the element is already present.
  464.         '''
  465.         
  466.         try:
  467.             self._data[element] = True
  468.         except TypeError:
  469.             transform = getattr(element, '_as_immutable', None)
  470.             if transform is None:
  471.                 raise 
  472.             
  473.             self._data[transform()] = True
  474.  
  475.  
  476.     
  477.     def remove(self, element):
  478.         '''Remove an element from a set; it must be a member.
  479.  
  480.         If the element is not a member, raise a KeyError.
  481.         '''
  482.         
  483.         try:
  484.             del self._data[element]
  485.         except TypeError:
  486.             transform = getattr(element, '_as_temporarily_immutable', None)
  487.             if transform is None:
  488.                 raise 
  489.             
  490.             del self._data[transform()]
  491.  
  492.  
  493.     
  494.     def discard(self, element):
  495.         '''Remove an element from a set if it is a member.
  496.  
  497.         If the element is not a member, do nothing.
  498.         '''
  499.         
  500.         try:
  501.             self.remove(element)
  502.         except KeyError:
  503.             pass
  504.  
  505.  
  506.     
  507.     def pop(self):
  508.         '''Remove and return an arbitrary set element.'''
  509.         return self._data.popitem()[0]
  510.  
  511.     
  512.     def _as_immutable(self):
  513.         return ImmutableSet(self)
  514.  
  515.     
  516.     def _as_temporarily_immutable(self):
  517.         return _TemporarilyImmutableSet(self)
  518.  
  519.  
  520.  
  521. class _TemporarilyImmutableSet(BaseSet):
  522.     
  523.     def __init__(self, set):
  524.         self._set = set
  525.         self._data = set._data
  526.  
  527.     
  528.     def __hash__(self):
  529.         return self._set._compute_hash()
  530.  
  531.  
  532.